package kr.saeildamil.androidStudy_second.multimedia;

import kr.saeildamil.androidStudy_second.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.MediaColumns;
import android.provider.MediaStore.Video;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class DumpMedia extends Activity {
	ContentResolver mCr;
	TextView mResult;
	RadioGroup mMedia;
	ToggleButton mStorage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_dumpmedia);

		mCr = getContentResolver();
		mResult = (TextView) findViewById(R.id.ch33_result);
		mStorage = (ToggleButton) findViewById(R.id.storage);
		mMedia = (RadioGroup) findViewById(R.id.media);
		
		mMedia.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				dumpQuery();
			}
		});
		
		mStorage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				dumpQuery();
			}
		});
		
		dumpQuery();
		
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
		filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
		filter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
		filter.addDataScheme("file");
		registerReceiver(mScanReceiver, filter);
	}
	
	BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
		public void onReceive(Context arg0, Intent arg1) {
			dumpQuery();
		}
	};
	
	protected void onPause() {
		super.onPause();
		unregisterReceiver(mScanReceiver);
	};

	public void dumpQuery() {
		StringBuilder sb = new StringBuilder();
		Uri uri;
		boolean inStorage = mStorage.isChecked();

		// 미디어 종류와 메모리 위치로부터 Uri 결정
		switch (mMedia.getCheckedRadioButtonId()) {
		case R.id.image:
		default:
			uri = inStorage ? Images.Media.INTERNAL_CONTENT_URI
					: Images.Media.EXTERNAL_CONTENT_URI;
			break;
		case R.id.audio:
			uri = inStorage ? Audio.Media.INTERNAL_CONTENT_URI
					: Audio.Media.EXTERNAL_CONTENT_URI;
			break;
		case R.id.video:
			uri = inStorage ? Video.Media.INTERNAL_CONTENT_URI
					: Video.Media.EXTERNAL_CONTENT_URI;
			break;
		}

		Cursor cursor = mCr.query(uri, null, null, null, null); // select * from uri

		// 필드 목록 출력
		int recordCount = cursor.getColumnCount();	//필드 갯수
		sb.append("필드 목록의 수 = " + recordCount + "\n\n");
		for (int i = 0; i < recordCount; i++) {
			sb.append(i);
			sb.append(i + " : " + cursor.getColumnName(i) + "\n");
		}
		sb.append("\n======================================\n\n");
		
		//실제 레코드 값 출력
		sb.append("레코드의 갯수 = " + cursor.getCount() + "\n\n");	
		while(cursor.moveToNext()){
			sb.append(getColumeValue(cursor, MediaColumns._ID));
			sb.append(getColumeValue(cursor, MediaColumns.DISPLAY_NAME));
			sb.append(getColumeValue(cursor, MediaColumns.TITLE));
			sb.append(getColumeValue(cursor, MediaColumns.SIZE));
			sb.append(getColumeValue(cursor, MediaColumns.DATE_ADDED));
			sb.append(getColumeValue(cursor, MediaColumns.MIME_TYPE));
			
			switch(mMedia.getCheckedRadioButtonId()){
			case R.id.image:
				sb.append(getColumeValue(cursor, Images.ImageColumns.BUCKET_DISPLAY_NAME));
				sb.append(getColumeValue(cursor, Images.ImageColumns.BUCKET_ID));
				sb.append(getColumeValue(cursor, Images.ImageColumns.DATE_TAKEN));
				sb.append(getColumeValue(cursor, Images.ImageColumns.DESCRIPTION));
				sb.append(getColumeValue(cursor, Images.ImageColumns.IS_PRIVATE));
				sb.append(getColumeValue(cursor, Images.ImageColumns.LATITUDE));
				sb.append(getColumeValue(cursor, Images.ImageColumns.LONGITUDE));
				sb.append(getColumeValue(cursor, Images.ImageColumns.MINI_THUMB_MAGIC));
				sb.append(getColumeValue(cursor, Images.ImageColumns.ORIENTATION));
				break;
			case R.id.audio:
				sb.append(getColumeValue(cursor, Audio.AudioColumns.ALBUM));
				sb.append(getColumeValue(cursor, Audio.AudioColumns.ARTIST));
				sb.append(getColumeValue(cursor, Audio.AudioColumns.YEAR));
				sb.append(getColumeValue(cursor, Audio.AudioColumns.DURATION));
				break;
			case R.id.video:
				sb.append(getColumeValue(cursor, Video.VideoColumns.DURATION));
				sb.append(getColumeValue(cursor, Video.VideoColumns.RESOLUTION));
				break;
			}

		}	
		sb.append("\n");
		mResult.setText(sb.toString());
	}
	
	private String getColumeValue(Cursor cursor, String cName){
		return cName + " : " + cursor.getString(cursor.getColumnIndex(cName)) + "\n";
	}

}
